<?php
namespace Admin\Controller;
use Think\Controller;
class MaintainController extends CommonController{
	//维护列表
	public function Index(){
		session('page_size',$_GET['page_size']);
        $shop = M('shop')->where('del=0')->select();
        $shop_id = $_GET['shop_id'] ? $_GET['shop_id'] : 1;
        $this->assign('shop_id',$shop_id);
        $page_no = I('get.p') ? I('get.p') : 1;
        $page_size = I('get.page_size') ? I('get.page_size') : 10;
        $data = M('maintain a')
            ->field('a.id,a.detail,a.status,a.reason,b.name,c.cate_name')
            ->where('b.shop_id='.$shop_id)
            ->join('left join zm_device b on b.id=a.device_id')
            ->join('left join zm_device_cate c on c.id=b.cate_id')
            ->page($page_no, $page_size)
            ->select();
        $count =M('maintain a')->join('left join zm_device b on b.id=a.device_id')->where('b.shop_id='.$shop_id)->count();
        $Page = new \AntAge\Page($count, $page_size);
        $show = $Page->show();
        $this->assign('type',$_SESSION['type']);
        $this->assign('shop', $shop);
        $this->assign('page', $show);
        $this->assign("count", $count);
        $this->assign("data", $data);
        $this->display();
	}
    //设备状态查看
    public function StatusCheck(){
        session('page_size',$_GET['page_size']);
        $page_no = I('get.p') ? I('get.p') : 1;
        $page_size = I('get.page_size') ? I('get.page_size') : 10;
        $shop = M('shop')->where('del=0')->select();
        $this->assign('shop',$shop);
        //找到所有的一级状态值
        $one_status = M('device_status')->getField('id,status_name');
        //找到所有的二级状态值
        $two_status = M('device_status_next')->getField('id,name');
        $where=array();
        if ($_GET['name']) {
            $where['name']=array('like', '%' . $_GET['name'] . '%');
            $this->assign('name',$where['name']);
        }
        $shop_id = $_GET['shop_id'] ? $_GET['shop_id'] : 1;
        $where['a.shop_id'] = $shop_id;
        
        $this->assign('shop_id',$shop_id);
        if (count($where)>0) {
             $data = M('status_log a')
            ->field('a.id,a.device_id,a.user_id,a.status_id,a.is_normal,a.twostatus_id,b.name')
            ->where($where)
            ->join("left join zm_device b on a.device_id=b.id")
            ->page($page_no, $page_size)
            ->select();
            $count =M('status_log a')->where($where)->count();
        }else{
            $data = M('status_log a')
            ->field('a.id,a.device_id,a.user_id,a.status_id,a.is_normal,a.twostatus_id,b.name')
            ->join("left join zm_device b on a.device_id=b.id")
            ->page($page_no, $page_size)
            ->select();
            $count =M('status_log a')->where()->count();
        }
       
        //将状态数据里面的状态id和状态名字对应起来
        foreach ($data as $key => $value) {
            $one_temp = "";$two_temp="";
            $one_status_ids = explode(",",$value['status_id']);
            $two_status_ids = explode(",",$value['twostatus_id']);
            foreach ($one_status as $one_status_key => $one_status_value) {
                if(in_array($one_status_key,$one_status_ids)){
                    $one_temp=$one_temp.$one_status_value.",";
                }
            }
            foreach ($two_status as $two_status_key => $two_status_value) {
                if(in_array($two_status_key,$two_status_ids)){
                    $two_temp=$two_temp.$two_status_value.",";
                }
            }
            $data[$key]['status_id']=$one_temp;
            $data[$key]['twostatus_id']=$two_temp;
        }
        $Page = new \AntAge\Page($count, $page_size);
        $show = $Page->show();
        $this->assign('type',$_SESSION['type']);
        $this->assign('page', $show);
        $this->assign("count", $count);
        $this->assign("data", $data);
        $this->display();
    }
    public function StatusUp(){
        $id = I('id');
        if (!$id) {
            $this->error('参数错误');
        }
        $cate_id = M('device')->where('id='.$id)->getField('cate_id');
        $this->assign('cate',$cate_id);
        
    }
    public function Detail(){
        $id = I('id');
        if (!$id) {
            $this->error('参数错误');
        }
        $data = M('status_log a')
            ->field('a.id,a.device_id,a.user_id,a.status_id,a.is_normal,a.twostatus_id,a.add_time,b.name')
            ->where('a.id='.$id)
            ->join("left join zm_device b on a.device_id=b.id")
            ->order('id desc')
            ->find();
        //找到所有的一级状态值
        $one_status = M('device_status')->getField('id,status_name');
        //找到所有的二级状态值
        $two_status = M('device_status_next')->getField('id,name');
        
        $one_status_ids = explode(",",$data['status_id']);
        $two_status_ids = explode(",",$data['twostatus_id']);
        $one_temp = "";$two_temp="";
        foreach ($one_status as $one_status_key => $one_status_value) {
            if(in_array($one_status_key,$one_status_ids)){
                $one_temp=$one_temp.$one_status_value.",";
            }
        }
        foreach ($two_status as $two_status_key => $two_status_value) {
            if(in_array($two_status_key,$two_status_ids)){
                $two_temp=$two_temp.$two_status_value.",";
            }
        }

        if(!empty($data)){
            $img = M('maintain')
                ->field('img')->where('device_id='.$data['device_id'])
                ->order('id desc')->find();

            $img = $img['img'];
            $data['img'] = $img;
            $unnor_photo = M('unnormal_photo')->field('photo_id,status_id')->where(array('device_id' => $data['device_id'], 'add_time' => $data['add_time']))->find();
            if($unnor_photo){
                $unnor_photo['status_str'] = M('device_status_next')->where(array('id'=>$unnor_photo['status_id']))->getField('name');
                $this->assign('unnor',$unnor_photo);
            }
        }
        


        $data['one_sta'] = $one_temp;
        $data['two_sta'] = $two_temp;
        $this->assign('type',$_SESSION['type']);
        $this->assign('data',$data);
        $this->display();
    }

    public function photos(){
        session('page_size',$_GET['page_size']);
        $shop = M('shop')->where('del=0')->select();
        $shop_id = $_GET['shop_id'] ? $_GET['shop_id'] : 1;
        $this->assign('shop_id',$shop_id);
        $this->assign('shop',$shop);
        //找到所有的一级状态值
        $one_status = M('device_status')->getField('id,status_name');
        //找到所有的二级状态值
        $two_status = M('device_status_next')->getField('id,name');

        $page_no = I('get.p') ? I('get.p') : 1;
        $page_size = I('get.page_size') ? I('get.page_size') : 10;
        $where['b.shop_id'] = $shop_id;
        $data = M('unnormal_photo a')
                ->field('a.id,a.device_id,a.photo_id,b.status_id,a.add_time,b.twostatus_id')
                ->join('left join zm_status_log b on a.device_id=b.device_id and a.add_time=b.add_time')
                ->where($where)
                ->page($page_no, $page_size)
                ->select();

        foreach ($data as $key => $value) {
            $one_temp = "";$two_temp="";
            $one_status_ids = explode(",",$value['status_id']);
            $two_status_ids = explode(",",$value['twostatus_id']);
            foreach ($one_status as $one_status_key => $one_status_value) {
                if(in_array($one_status_key,$one_status_ids)){
                    $one_temp=$one_temp.$one_status_value.",";
                }
            }
            foreach ($two_status as $two_status_key => $two_status_value) {
                if(in_array($two_status_key,$two_status_ids)){
                    $two_temp=$two_temp.$two_status_value.",";
                }
            }
            $data[$key]['status_id']=$one_temp;
            $data[$key]['twostatus_id']=$two_temp;
        }
        $count = M('unnormal_photo a')->join('left join zm_status_log b on a.device_id=b.device_id and a.add_time=b.add_time')->where($where)->count();
        $Page = new \AntAge\Page($count, $page_size);
        $show = $Page->show();
        $this->assign('type',$_SESSION['type']);
        $this->assign('data',$data);
        $this->display();
    }
}